#! /usr/bin/sdlbrt

'- picture converter - Mattel Aquarius - colour version (based on a zx81 picture converter) - Paulo Silva - Jan'11 - GPL licence

finp$="picture.jpg"

finp2$="_tmpr_m.png":shell("convert -modulate 100,0 "+finp$+" "+finp2$)
finp3$="_tmpr_n.png":shell("convert "+finp$+" "+finp3$)

onlgr=0
xed=0:yed=0
open finp2$ for input as #1
  for i=1 to 16:n=readbyte(1):next:for i=1 to 2:n=readbyte(1):next
  xed=readbyte(1)*256:xed=xed+readbyte(1)
  for i=1 to 2:n=readbyte(1):next:yed=readbyte(1)*256:yed=yed+readbyte(1)
  close #1

xed2a=xed*4:xed2disp=xed2a:if xed2disp<(160+xed) then: xed2disp=160+xed:end if
yed2a=yed*(8/3):yed2disp=yed2a+128:if yed2disp<(yed2a+yed) then:yed2disp=yed2a+yed:end if
xed2disp2=xed*8:yed2disp2=yed*(16/3)

setdisplay(xed2disp2,yed2disp2,32,1):paper(0x808080):ink(0):pen(0):cls

loadimage(finp2$,1):pasteicon(160,yed2a+72,1)
loadimage(finp3$,1):pasteicon(160+80,yed2a,1)

sub filpic7(qxx1,qyy1,qstq$)
  yyt=qyy1:xxt=qxx1:tstt$=qstq$
  for qq=0 to len(tstt$)-1
    qu=(((val(mid$(tstt$,qq+1,1)))*255)/7):ink(rgb(qu,qu,qu)):dot(xxt+qq,yyt):next
  end sub

dim mapch[40,25]:dim mapat[40,25]

dim paleta[16]
paleta[00]=0x111111:paleta[01]=0xFF1111:paleta[02]=0x11FF11:paleta[03]=0xFFEE11
paleta[04]=0x2222DD:paleta[05]=0xFF11FF:paleta[06]=0x33CCCC:paleta[07]=0xFFFFFF
paleta[08]=0xCCCCCC:paleta[09]=0x22AAAA:paleta[10]=0xCC22CC:paleta[11]=0x441188
paleta[12]=0xFFFF77:paleta[13]=0x22C844:paleta[14]=0xAA2222:paleta[15]=0x333333

filpic7(0,yed2a+000,"77000077707770777077707770077077777777777700007777777777777777777770077777700777777700007777777777777777000077777777777700000077")
filpic7(0,yed2a+001,"77077777707707777077077777070777777077777077770777777077770777777700007777700777777770007777707777077777000777777700007700000077")
filpic7(0,yed2a+002,"77077777707077777070777770707777777777770770077077777707707777777070070777700777777700007777000770007777000077777700007777000077")
filpic7(0,yed2a+003,"70000777770700777707007777070077700000770707777000000000000000007770077777700777777000707770007777000777070007777777777777007777")
filpic7(0,yed2a+004,"77077777707770777070707770707077777777770707777000000000000000007770077777700777770007770700077777700070777000777000000777007777")
filpic7(0,yed2a+005,"70077777777707777770007777700077777077770770077077777707707777777770077770700707700077770000777777770000777700070000000077007777")
filpic7(0,yed2a+006,"07070077777000777777707777777077777777777077770777777077770777777770077777000077770777770007777777777000777770770000000077007777")
filpic7(0,yed2a+007,"77777777777777777777777777777777777777777700007777777777777777777770077777700777777777770000777777770000777777770000000077007777")
filpic7(0,yed2a+008,"00000000700000007700007777777777700000077777777770000077777777777000007777700777777777777777000077777777000077770000777777777777")
filpic7(0,yed2a+009,"77000077700000007700007777000077770000777700077777000007770007777700000077000077777777777777000077777777000077770000777777777777")
filpic7(0,yed2a+010,"77000077700007777700007777000077700770077700007777700700770000777770000070077007777777777777000077777777000077770000777777777777")
filpic7(0,yed2a+011,"77000077777007777700007777777777700770077777777777700007777777777700070077077077777777777777000077777777000077770000777777777777")
filpic7(0,yed2a+012,"77000077777007777700007770000007000770007700077777700077770007700000770000077000777700007777777700007777777777777777000000000000")
filpic7(0,yed2a+013,"77777777777007777700007700000000007777007000077777700777700007700077770007000070777700007777777700007777777777777777000000000000")
filpic7(0,yed2a+014,"77777777777007777700007707000070007777007000007777000777007000000777777707700770777700007777777700007777777777777777000000000000")
filpic7(0,yed2a+015,"77777777777007777700007700700700007777007000000077000777007000777777777700700700777700007777777700007777777777777777000000000000")
filpic7(0,yed2a+016,"77777777777077777707077777070777777077777007777777077777777707777770777777707777777077777777777777777777777777777777777777777777")
filpic7(0,yed2a+017,"77777777777077777707077777070777770000777007707770707777777707777707777777770777707070777770777777777777777777777777777777777077")
filpic7(0,yed2a+018,"77777777777077777707077770000077707077777777077770707777777077777077777777777077770007777770777777777777777777777777777777770777")
filpic7(0,yed2a+019,"77777777777077777777777777070777770007777770777777077777777777777077777777777077777077777000007777777777700000777777777777707777")
filpic7(0,yed2a+020,"77777777777077777777777770000077777070777707777770707077777777777077777777777077770007777770777777707777777777777777777777077777")
filpic7(0,yed2a+021,"77777777777777777777777777070777700007777077007770770777777777777707777777770777707070777770777777707777777777777777777770777777")
filpic7(0,yed2a+022,"77777777777077777777777777070777777077777777007777007077777777777770777777707777777077777777777777077777777777777770777777777777")
filpic7(0,yed2a+023,"77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777")
filpic7(0,yed2a+024,"77000777777077777700077770000077777707777000007777700077700000777700077777000777777777777777777777770777777777777707777777000777")
filpic7(0,yed2a+025,"70777077770077777077707777777077777007777077777777077777777770777077707770777077777777777777777777707777777777777770777770777077")
filpic7(0,yed2a+026,"70770077777077777777707777770777770707777000077770777777777707777077707770777077777077777770777777077777700000777777077777770777")
filpic7(0,yed2a+027,"70707077777077777770077777700777707707777777707770000777777077777700077777000077777777777777777770777777777777777777707777707777")
filpic7(0,yed2a+028,"70077077777077777707777777777077700007777777707770777077770777777077707777777077777077777770777777077777700000777777077777707777")
filpic7(0,yed2a+029,"70777077777077777077777770777077777707777077707770777077770777777077707777770777777777777770777777707777777777777770777777777777")
filpic7(0,yed2a+030,"77000777770007777000007777000777777707777700077777000777770777777700077770007777777777777707777777770777777777777707777777707777")
filpic7(0,yed2a+031,"77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777")
filpic7(0,yed2a+032,"77000777777077777000077777000777700007777000007770000077770000777077707777000777777770777077707770777777707770777077707777000777")
filpic7(0,yed2a+033,"70777077770707777077707770777077707770777077777770777777707777777077707777707777777770777077077770777777700700777077707770777077")
filpic7(0,yed2a+034,"70707077707770777077707770777777707770777077777770777777707777777077707777707777777770777070777770777777707070777007707770777077")
filpic7(0,yed2a+035,"70700077707770777000077770777777707770777000077770000777707777777000007777707777777770777007777770777777707070777070707770777077")
filpic7(0,yed2a+036,"70700777700000777077707770777777707770777077777770777777707700777077707777707777777770777070777770777777707770777077007770777077")
filpic7(0,yed2a+037,"70777777707770777077707770777077707770777077777770777777707770777077707777707777707770777077077770777777707770777077707770777077")
filpic7(0,yed2a+038,"77000777707770777000077777000777700007777000007770777777770000777077707777000777770007777077707770000077707770777077707777000777")
filpic7(0,yed2a+039,"77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777")
filpic7(0,yed2a+040,"70000777770007777000077777000777700000777077707770777077707770777077707770777077700000777000007777777777700000777777777777777777")
filpic7(0,yed2a+041,"70777077707770777077707770777077777077777077707770777077707770777077707770777077777770777007777770777777777700777777777777777777")
filpic7(0,yed2a+042,"70777077707770777077707770777777777077777077707770777077707770777707077777070777777707777007777777077777777700777770777777777777")
filpic7(0,yed2a+043,"70000777707770777000077777000777777077777077707770777077707070777770777777707777777077777007777777707777777700777707077777777777")
filpic7(0,yed2a+044,"70777777707070777070777777777077777077777077707770777077707070777707077777707777770777777007777777770777777700777077707777777777")
filpic7(0,yed2a+045,"70777777707707777077077770777077777077777077707777070777700700777077707777707777707777777007777777777077777700777777777777777777")
filpic7(0,yed2a+046,"70777777770070777077707777000777777077777700077777707777707770777077707777707777700000777000007777777777700000777777777770000077")
filpic7(0,yed2a+047,"77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777")
filpic7(0,yed2a+048,"77077777777777777077777777777777777770777777777777770777777777777077777777707777777707777077777777007777777777777777777777777777")
filpic7(0,yed2a+049,"77077777777777777077777777777777777770777777777777707777777777777077777777777777777777777077777777707777777777777777777777777777")
filpic7(0,yed2a+050,"77707777770070777070077777700077770070777700077777707777770070777000077777007777777707777077077777707777700700777000077777000777")
filpic7(0,yed2a+051,"77777777707700777007707777077777707700777077707777000777707700777077707777707777777707777070777777707777707077077077707770777077")
filpic7(0,yed2a+052,"77777777707770777077707777077777707770777000007777707777707770777077707777707777777707777000777777707777707077077077707770777077")
filpic7(0,yed2a+053,"77777777707700777007707777077777707700777077777777707777770000777077707777707777777707777077077777707777707077077077707770777077")
filpic7(0,yed2a+054,"77777777770070777070077777700077770070777700077777707777777770777077707777000777777707777077707777000777707077077077707777000777")
filpic7(0,yed2a+055,"77777777777777777777777777777777777777777777777777777777770007777777777777777777770077777777777777777777777777777777777777777777")
filpic7(0,yed2a+056,"77777777777777777777777777777777777077777777777777777777777777777777777777777777777777777777007777707777700777777777777700000000")
filpic7(0,yed2a+057,"77777777777777777777777777777777777077777777777777777777777777777777777777777777777777777770777777707777777077777777777700000000")
filpic7(0,yed2a+058,"70700777770070777070077777000077700000777077707770777077707077077077707777077077700000777770777777707777777077777777707700000000")
filpic7(0,yed2a+059,"70077077707700777007777770777777777077777077707770777077707077077707077777077077777707777707777777777777777707777700077700000000")
filpic7(0,yed2a+060,"70777077707770777077777777000777777077777077707777070777707077077770777777077077777077777770777777707777777077777077777700000000")
filpic7(0,yed2a+061,"70077077707700777077777777777077777077777077707777070777707077077707077777000077770777777770777777707777777077777777777700000000")
filpic7(0,yed2a+062,"70700777770070777077777770000777777077777700007777707777770700777077707777777077700000777777007777707777700777777777777700000000")
filpic7(0,yed2a+063,"70777777777770077777777777777777777777777777777777777777777777777777777777000777777777777777777777777777777777777777777700000000")
filpic7(0,yed2a+064,"77777777077777777777777777000007777777770707777707070707777777777777777777777777777007777700007777777777770000070077777777700777")
filpic7(0,yed2a+065,"00000000077777777770007770000077777777777070777770707070777007777777777777777777777007777770077777700077000000770000777777700777")
filpic7(0,yed2a+066,"00000000077777777700007700700777777777770707777707070707770000777777777700000000770000770070070077000077000007770000007777000077")
filpic7(0,yed2a+067,"00000000077777777777777770000777777777777070777770707070700000077777777700000000700000070000000077777777007000770000000077000077")
filpic7(0,yed2a+068,"00000000077777777770007777000777070707070707777707070707700000077777777700000000000000007000000707700077007700000000000070000007")
filpic7(0,yed2a+069,"00000000077777777770000777700777707070707070777770707070770000777777777700000000007007007700007707700007007777000000007770000007")
filpic7(0,yed2a+070,"00000000077777777700000777700077070707070707777707070707777007770000000000000000777007777770077700000700777777700000777700000000")
filpic7(0,yed2a+071,"00000000077777770000000777700077707070707070777770707070777777770000000000000000770000777770077777000700777777770077777700000000")
filpic7(0,yed2a+072,"77777777000000077700007777000077070707077777070777000077007777770007777700007777770077777777007777777777777777777777770000000000")
filpic7(0,yed2a+073,"77777777000000077070770770770707707070707777707070000007007777770007777700007777770007777770007770077007777007777777000000000000")
filpic7(0,yed2a+074,"77777777000000077700007777000077070707077777070700000000007777770007777700007777077000777700077070000007000000007700000070000007")
filpic7(0,yed2a+075,"77777777000000070777777777777770707070777777707000000000007777770007777700007777000000000000000070777707007777000000000070000007")
filpic7(0,yed2a+076,"77777777000000070700007777000070777777777777070700000000007777770007777700007777000000000000000000777700707777070000000077000077")
filpic7(0,yed2a+077,"77777777000000070000000000000000777777777777707000000000007777770007777700007777077000777700077000000000700000077700000077000077")
filpic7(0,yed2a+078,"77777777000000077700007007000077777777777777070770000007007777770007777700007777770007777770007777700777700770077777000077700777")
filpic7(0,yed2a+079,"00000000000000077700007007000077777777777777707077000077007777770007777700007777770077777777007777777777777777777777770077700777")
filpic7(0,yed2a+080,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+081,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+082,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+083,"77777777777777777777777777777777000077770000777700007777000077777777000077770000777700007777000000000000000000000000000000000000")
filpic7(0,yed2a+084,"77777777777777777777777777777777000077770000777700007777000077777777000077770000777700007777000000000000000000000000000000000000")
filpic7(0,yed2a+085,"77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777")
filpic7(0,yed2a+086,"77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777")
filpic7(0,yed2a+087,"77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777")
filpic7(0,yed2a+088,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+089,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+090,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+091,"77777777777777777777777777777777000077770000777700007777000077777777000077770000777700007777000000000000000000000000000000000000")
filpic7(0,yed2a+092,"77777777777777777777777777777777000077770000777700007777000077777777000077770000777700007777000000000000000000000000000000000000")
filpic7(0,yed2a+093,"00007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777")
filpic7(0,yed2a+094,"00007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777")
filpic7(0,yed2a+095,"00007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777")
filpic7(0,yed2a+096,"77777770077777770000000000000077777777777770077777777777777700007770077777777777777777707777770077700777777007777777777777700777")
filpic7(0,yed2a+097,"77777700007777770000000000000077777777777700007777777777777700007770077777777777777777077777700077700777777007777777777777700777")
filpic7(0,yed2a+098,"77777000000777777000000700000077770000777000000777777777777770007770077777777777777770777777000077700777777007777777777777700777")
filpic7(0,yed2a+099,"77770000000077777700007700000077770000770000000077700777777777000000000077777777777707777777000000000000777000000000077777700000")
filpic7(0,yed2a+100,"77700000000007777777777700000077770000770000000077700777777777770000000000777777777077777777000000000000777000000000077777700000")
filpic7(0,yed2a+101,"77000000000000777777777700000077770000777000000777777777777777777770077700077777770777777777000077777777777007777770077777777777")
filpic7(0,yed2a+102,"70000000000000077777777700000077777777777700007777777777777777777770077700007777707777777777700077777777777007777770077777777777")
filpic7(0,yed2a+103,"00000000000000007777777700000077777777777770077777777777777777777770077700007777077777777777770077777777777007777770077777777777")
filpic7(0,yed2a+104,"77770770707077077777777777777777700770077770077777700777777777770777777000007777077777770077777777777777777007777777777777700777")
filpic7(0,yed2a+105,"77077777707770777777777777707777000000007700007777700777777777777077770700007777707777770007777777777777777007777777777777700777")
filpic7(0,yed2a+106,"77777077770700707777777777070077000000007770077777700777777777777707707700077777770777770000777777777777777007777777777777700777")
filpic7(0,yed2a+107,"07777777007770777777777777000707000000007077770777700777777777777770077700777777777077770000777700000000000007777770000000000777")
filpic7(0,yed2a+108,"77707770777077707700007770700077700000070007700077700777777777007770077777777777777707770000777700000000000007777770000000000777")
filpic7(0,yed2a+109,"70777777070070777000000777007077770000777077770777700777777770007707707777777777777770770000777777700777777007777770077777777777")
filpic7(0,yed2a+110,"77770777770777000000000077777077777007777770077777700777777000007077770777777777777777070007777777700777777007777770077777777777")
filpic7(0,yed2a+111,"77777707707707070000000077777777777007777700007777700777777000000777777077777777777777700077777777700777777007777770077777777777")
filpic7(0,yed2a+112,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+113,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+114,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+115,"77777777777777777777777777777777000077770000777700007777000077777777000077770000777700007777000000000000000000000000000000000000")
filpic7(0,yed2a+116,"77777777777777777777777777777777000077770000777700007777000077777777000077770000777700007777000000000000000000000000000000000000")
filpic7(0,yed2a+117,"77770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000")
filpic7(0,yed2a+118,"77770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000")
filpic7(0,yed2a+119,"77770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000777700007777000077770000")
filpic7(0,yed2a+120,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+121,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+122,"77777777000077777777000000000000777777770000777777770000000000007777777700007777777700000000000077777777000077777777000000000000")
filpic7(0,yed2a+123,"77777777777777777777777777777777000077770000777700007777000077777777000077770000777700007777000000000000000000000000000000000000")
filpic7(0,yed2a+124,"77777777777777777777777777777777000077770000777700007777000077777777000077770000777700007777000000000000000000000000000000000000")
filpic7(0,yed2a+125,"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
filpic7(0,yed2a+126,"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
filpic7(0,yed2a+127,"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")

filpic7(128,yed2a+000,"45555555674566665555717667175514"):filpic7(128,yed2a+001,"47555555564543345555544554453346")
filpic7(128,yed2a+002,"56657676774566665555671771761147"):filpic7(128,yed2a+003,"22214455335633563255777077070777")
filpic7(128,yed2a+004,"55554433334553453233747447474444"):filpic7(128,yed2a+005,"77554422442345225633707707777000")
filpic7(128,yed2a+006,"77575555454657765766566777777776"):filpic7(128,yed2a+007,"77677745466656775775465667667767")
filpic7(128,yed2a+008,"77677766677556776767677767776777"):filpic7(128,yed2a+009,"55575555654555555555777766776755")
filpic7(128,yed2a+010,"55576665555556675565676757557667"):filpic7(128,yed2a+011,"56565656765656675657776776776767")
filpic7(128,yed2a+012,"45564556454545565556755657455555"):filpic7(128,yed2a+013,"45555557555757565557754757555555")
filpic7(128,yed2a+014,"56665656565667556656566656666656"):filpic7(128,yed2a+015,"45554555465555555555553667547777")
filpic7(128,yed2a+016,"56554656575555455657674766746677"):filpic7(128,yed2a+017,"67566656675667666667564677557756")
filpic7(128,yed2a+018,"57775777757766775767765757676777"):filpic7(128,yed2a+019,"77554556554557555557754657455555")
filpic7(128,yed2a+020,"77565666565667556656666656566656"):filpic7(128,yed2a+021,"67776777577777677777776657577700")
filpic7(128,yed2a+022,"45555755565555455655565767676600"):filpic7(128,yed2a+023,"56556756675567656655567667577700")
filpic7(128,yed2a+024,"33576533774744667766554465232655"):filpic7(128,yed2a+025,"00575435554744337700222254230233")
filpic7(128,yed2a+026,"00573254444744662200445522652611"):filpic7(128,yed2a+027,"77024554447422472707355355556211")
filpic7(128,yed2a+028,"77023443567400472707222233332033"):filpic7(128,yed2a+029,"44023223777422472707355355556255")
filpic7(128,yed2a+030,"77177111670761017616701066066000"):filpic7(128,yed2a+031,"77577555472745257454725244244222")
filpic7(128,yed2a+032,"77777777676767677676767666666666"):filpic7(128,yed2a+033,"77177111670761017616701066066000")
filpic7(128,yed2a+034,"57475545270725055444524224042202"):filpic7(128,yed2a+035,"17171717070707071616161606060606")
filpic7(128,yed2a+036,"74471104664477715577757255556755"):filpic7(128,yed2a+037,"61165504441166753357667033533663")
filpic7(128,yed2a+038,"20027704664477775517577266555576"):filpic7(128,yed2a+039,"65557766225555775517572766557655")
filpic7(128,yed2a+040,"66455544225555755557660733356336"):filpic7(128,yed2a+041,"75541166554455615577752755555567")
filpic7(128,yed2a+042,"77177111670761017616701066066000"):filpic7(128,yed2a+043,"75557454452544247252705042224020")
filpic7(128,yed2a+044,"71717171616161617070707060606060"):filpic7(128,yed2a+045,"77177111670761017616701066066000")
filpic7(128,yed2a+046,"55455444250524045242504022022000"):filpic7(128,yed2a+047,"11111111010101011010101000000000")

for y1=0 to 71 step 3:for x1=0 to 79 step 2
  rac=0:gac=0:bac=0
  for y2=0 to 2: for x2=0 to 1
    x=x1+x2:y=y1+y2
    u=point(x+160+80,y+yed2a)
    b=u mod 256: u=int(u/256)
    g=u mod 256: u=int(u/256)
    r=u mod 256: u=int(u/256)
    rac+=r:gac+=g:bac+=b
    next:next
  rav=rac/6:gav=gac/6:bav=bac/6
  ink(rgb(rav,gav,bav)):bar(x1+160+80,y1+yed2a+72,x1+160+80+1,y1+yed2a+72+2)
  next:next

for y1=0 to 23:for x1=0 to 39
  dist=9999
  u1=point(160+80+(x1*2),yed2a+72+(y1*3))
  b1=u1 mod 256:u1=int(u1/256)
  g1=u1 mod 256:u1=int(u1/256)
  r1=u1 mod 256:u1=int(u1/256)
  pp=0
  for c=0 to 15
    u2=paleta[c]
    b2=u2 mod 256:u2=int(u2/256)
    g2=u2 mod 256:u2=int(u2/256)
    r2=u2 mod 256:u2=int(u2/256)
    rd=abs(r2-r1):gd=abs(g2-g1):bd=abs(b2-b1)
    distt=sqr((rd^2)+(gd^2)+(bd^2))
    if dist>distt then: dist=distt:pp=c:end if
    next
  ik=0
  u4=paleta[pp]
  dist=9999
  b4=u4 mod 256:u4=int(u4/256)
  g4=u4 mod 256:u4=int(u4/256)
  r4=u4 mod 256:u4=int(u4/256)
  for c=0 to 15
    if c<>pp then:
      u2=paleta[c]
      b2=u2 mod 256:u2=int(u2/256)
      g2=u2 mod 256:u2=int(u2/256)
      r2=u2 mod 256:u2=int(u2/256)
      r3=int((r4+r2)/2):g3=int((g4+g2)/2):b3=int((b4+b2)/2)
      rd=abs(r3-r1):gd=abs(g3-g1):bd=abs(b3-b1)
      distt=sqr((rd^2)+(gd^2)+(bd^2))
      if dist>distt then: dist=distt:ik=c:end if
      end if
    next

  ink (paleta[pp]):bar((xed*4)+x1*8,y1*8,7+(xed*4)+x1*8,7+(y1*8))
  ink (paleta[ik]):bar(4+(xed*4)+x1*8,1+(y1*8),6+(xed*4)+x1*8,5+(y1*8))

  mapat[x1,y1]=pp*16+ik

  next:next

for y1=0 to 23:for x1=0 to 39
  u1=point((xed*4)+(x1*8),y1*8)
  b1=u1 mod 256:u1=int(u1/256)
  g1=u1 mod 256:u1=int(u1/256)
  r1=u1 mod 256:u1=int(u1/256)
  w1=int(((b1*11)+(r1*30)+(g1*59))/100)
  u2=point(4+(xed*4)+(x1*8),2+(y1*8))
  b2=u2 mod 256:u2=int(u2/256)
  g2=u2 mod 256:u2=int(u2/256)
  r2=u2 mod 256:u2=int(u2/256)
  w2=int(((b2*11)+(r2*30)+(g2*59))/100)
  if w2<w1 then:tmz=w1:w1=w2:w2=tmz:end if
  dw=w2-w1:if dw<1 then:dw=1:end if
   
  for y2=0 to 2: for x2=0 to 1
    y=(y1*3)+y2:x=(x1*2)+x2
    u3=(point(x+160,y+yed2a+72))mod 256
    u4=int(((u3-w1)*255)/dw)
    if u4<0 then:u4=0:end if
    if u4>255 then:u4=255:end if
    ink(rgb(u4,u4,u4))
    dot(x+160,y+yed2a)
    next:next

  next:next

for y1=0 to (yed/3)-1:for x1=0 to (xed/2)-1:dfi=65536:fgx=0:fgy=0:chid=0

  for yq=0 to 15:for xq=0 to 15:cnt=0
    for y2=0 to 2:for x2=0 to 1
      q1=bitwiseand(point(160+(x1*2)+x2,yed2a+(y1*3)+y2),255)
      q2=bitwiseand(point(128+(xq*2)+x2,yed2a+0+(yq*3)+y2),255)
      dq=abs(q2-q1):cnt=cnt+dq
      next:next
    if cnt<dfi then:dfi=cnt:fgx=xq:fgy=yq:end if
    mapch[x1,y1]=fgy*16+fgx
    next:next
  for y2=0 to 7:for x2=0 to 7
    c=point((fgx*8)+x2,yed2a+(fgy*8)+y2):ink(c):dot((x1*8)+x2,(y1*8)+y2)
    ink(point((xed*4)+(x1*8),(y1*8)))
    if (c mod 256)>128 then:ink(point((xed*4)+(x1*8)+4,(y1*8)+2)):end if
    dot((x1*8)+x2+(xed*4),(y1*8)+y2+ (yed*8/3))
    next:next:next:next

'  fout$=finp$+".bmp":grab (1,0,0,xed2a,yed2a):saveimage (fout$,1)
  fout$=finp$+".bmp":grab (1,0,0,xed2disp2,yed2disp2):saveimage (fout$,1)

'- unix only (?) - needs imagemagick installed
  shell("convert "+fout$+" "+fout$+".png"):shell("rm "+fout$+" "+finp2$+" "+finp3$)  

    open finp$+".bin" for output as #1
      for y=0 to 23:for x=0 to 39
        writebyte(1,mapch[x,y])
        next:next
      for y=0 to 63:writebyte(1,0):next
      for y=0 to 23:for x=0 to 39
        writebyte(1,mapat[x,y])
        next:next
      close #1


waitkey

